<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Constructs an EvTimer watcher object</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="evtimer.again.html">EvTimer::again</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="evtimer.createstopped.html">EvTimer::createStopped</a></div>
 <div class="up"><a href="class.evtimer.html">EvTimer</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="evtimer.construct" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">EvTimer::__construct</h1>
  <p class="verinfo">(PECL ev &gt;= 0.2.0)</p><p class="refpurpose"><span class="refname">EvTimer::__construct</span> &mdash; <span class="dc-title">Constructs an EvTimer watcher object</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-evtimer.construct-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span>
   <span class="methodname"><strong>EvTimer::__construct</strong></span>
    ( <span class="methodparam">
    <span class="type">double</span>
     <code class="parameter">$after</code>
   </span>
   , <span class="methodparam">
    <span class="type">double</span>
     <code class="parameter">$repeat</code>
   </span>
   , <span class="methodparam">
    <span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span>
     <code class="parameter">$callback</code>
   </span>
   [, <span class="methodparam">
    <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span>
     <code class="parameter">$data</code>
    <span class="initializer"> = <strong><code>NULL</code></strong></span>
   </span>
   [, <span class="methodparam">
    <span class="type">int</span>
     <code class="parameter">$priority</code>
    <span class="initializer"> = 0</span>
   </span>
  ]] )</div>

  <p class="para rdfs-comment">
   Constructs an EvTimer watcher object.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-evtimer.construct-parameters">
  <h3 class="title">参数</h3>
  <dl>

   
    <dt>

     <em><code class="parameter">after</code></em>
    </dt>

    <dd>

     <p class="para">
      Configures the timer to trigger after
      <em><code class="parameter">after</code></em>
      seconds.
     </p>
    </dd>

   
   
    <dt>

     <em><code class="parameter">repeat</code></em>
    </dt>

    <dd>

     <p class="para">
      If repeat is
      <strong><code>0.0</code></strong>
      , then it will automatically be stopped once the timeout is reached. If
      it is positive, then the timer will automatically be configured to
      trigger again every repeat seconds later, until stopped manually.
     </p>
    </dd>

   
   
    <dt>

     <em><code class="parameter">callback</code></em>
    </dt>

    <dd>

     <p class="para">
      See
      <a href="ev.watcher-callbacks.html" class="link">Watcher callbacks</a>
      .
     </p>
    </dd>

   
   
    <dt>

     <em><code class="parameter">data</code></em>
    </dt>

    <dd>

     <p class="para">
      Custom data associated with the watcher.
     </p>
    </dd>

   
   
    <dt>

     <em><code class="parameter">priority</code></em>
    </dt>

    <dd>

     <p class="para">
      <a href="class.ev.html#ev.constants.watcher-pri" class="link">Watcher priority</a>
     </p>
    </dd>

   
  </dl>

 </div>

 <div class="refsect1 returnvalues" id="refsect1-evtimer.construct-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns EvTimer object on success.
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-evtimer.construct-examples">
  <h3 class="title">范例</h3>
 <div class="example" id="example-3540">
   <p><strong>Example #1 Simple timers</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Create&nbsp;and&nbsp;start&nbsp;timer&nbsp;firing&nbsp;after&nbsp;2&nbsp;seconds<br /></span><span style="color: #0000BB">$w1&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EvTimer</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;function&nbsp;()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"2&nbsp;seconds&nbsp;elapsed\n"</span><span style="color: #007700">;<br />});<br /><br /></span><span style="color: #FF8000">//&nbsp;Create&nbsp;and&nbsp;launch&nbsp;timer&nbsp;firing&nbsp;after&nbsp;2&nbsp;seconds&nbsp;repeating&nbsp;each&nbsp;second<br />//&nbsp;until&nbsp;we&nbsp;manually&nbsp;stop&nbsp;it<br /></span><span style="color: #0000BB">$w2&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EvTimer</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;function&nbsp;(</span><span style="color: #0000BB">$w</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"is&nbsp;called&nbsp;every&nbsp;second,&nbsp;is&nbsp;launched&nbsp;after&nbsp;2&nbsp;seconds\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"iteration&nbsp;=&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">iteration</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Stop&nbsp;the&nbsp;watcher&nbsp;after&nbsp;5&nbsp;iterations<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">iteration</span><span style="color: #007700">()&nbsp;==&nbsp;</span><span style="color: #0000BB">5&nbsp;</span><span style="color: #007700">and&nbsp;</span><span style="color: #0000BB">$w</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">stop</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Stop&nbsp;the&nbsp;watcher&nbsp;if&nbsp;further&nbsp;calls&nbsp;cause&nbsp;more&nbsp;than&nbsp;10&nbsp;iterations<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">iteration</span><span style="color: #007700">()&nbsp;&gt;=&nbsp;</span><span style="color: #0000BB">10&nbsp;</span><span style="color: #007700">and&nbsp;</span><span style="color: #0000BB">$w</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">stop</span><span style="color: #007700">();<br />});<br /><br /></span><span style="color: #FF8000">//&nbsp;Create&nbsp;stopped&nbsp;timer.&nbsp;It&nbsp;will&nbsp;be&nbsp;inactive&nbsp;until&nbsp;we&nbsp;start&nbsp;it&nbsp;ourselves<br /></span><span style="color: #0000BB">$w_stopped&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">EvTimer</span><span style="color: #007700">::</span><span style="color: #0000BB">createStopped</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">,&nbsp;function(</span><span style="color: #0000BB">$w</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Callback&nbsp;of&nbsp;a&nbsp;timer&nbsp;created&nbsp;as&nbsp;stopped\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Stop&nbsp;the&nbsp;watcher&nbsp;after&nbsp;2&nbsp;iterations<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">iteration</span><span style="color: #007700">()&nbsp;&gt;=&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">and&nbsp;</span><span style="color: #0000BB">$w</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">stop</span><span style="color: #007700">();<br />});<br /><br /></span><span style="color: #FF8000">//&nbsp;Loop&nbsp;until&nbsp;Ev::stop()&nbsp;is&nbsp;called&nbsp;or&nbsp;all&nbsp;of&nbsp;watchers&nbsp;stop<br /></span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">run</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;Start&nbsp;and&nbsp;look&nbsp;if&nbsp;it&nbsp;works<br /></span><span style="color: #0000BB">$w_stopped</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">start</span><span style="color: #007700">();<br />echo&nbsp;</span><span style="color: #DD0000">"Run&nbsp;single&nbsp;iteration\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">run</span><span style="color: #007700">(</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">RUN_ONCE</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Restart&nbsp;the&nbsp;second&nbsp;watcher&nbsp;and&nbsp;try&nbsp;to&nbsp;handle&nbsp;the&nbsp;same&nbsp;events,&nbsp;but&nbsp;don't&nbsp;block\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$w2</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">again</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">run</span><span style="color: #007700">(</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">RUN_NOWAIT</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$w&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EvTimer</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;function()&nbsp;{});<br />echo&nbsp;</span><span style="color: #DD0000">"Running&nbsp;a&nbsp;blocking&nbsp;loop\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">run</span><span style="color: #007700">();<br />echo&nbsp;</span><span style="color: #DD0000">"END\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  <div class="example-contents"><p>以上例程的输出类似于：</p></div>
  <div class="example-contents screen">
<div class="cdata"><pre>
2 seconds elapsed
is called every second, is launched after 2 seconds
iteration = 1
is called every second, is launched after 2 seconds
iteration = 2
is called every second, is launched after 2 seconds
iteration = 3
is called every second, is launched after 2 seconds
iteration = 4
is called every second, is launched after 2 seconds
iteration = 5
Run single iteration
Callback of a timer created as stopped
Restart the second watcher and try to handle the same events, but don&#039;t block
Running a blocking loop
is called every second, is launched after 2 seconds
iteration = 8
is called every second, is launched after 2 seconds
iteration = 9
is called every second, is launched after 2 seconds
iteration = 10
END
</pre></div>
  </div>
  </div>
 </div>

 <div class="refsect1 seealso" id="refsect1-evtimer.construct-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member">
    <span class="methodname"><a href="evtimer.createstopped.html" class="methodname" rel="rdfs-seeAlso">EvTimer::createStopped()</a> - Creates EvTimer stopped watcher object</span>
   </li>
   <li class="member">
    <a href="class.evperiodic.html" class="classname">EvPeriodic</a>
   </li>
   <li class="member">
    <a href="http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#code_ev_timer_code_relative_and_opti" class="link external">&raquo;&nbsp;ev_timer - relative and optionally repeating timeouts</a>
   </li>
   <li class="member">
    <a href="http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#Be_smart_about_timeouts" class="link external">&raquo;&nbsp;Be smart about timeouts</a>
   </li>
  </ul>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="evtimer.again.html">EvTimer::again</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="evtimer.createstopped.html">EvTimer::createStopped</a></div>
 <div class="up"><a href="class.evtimer.html">EvTimer</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
